'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');

var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _collapse = require('antd/lib/collapse');

var _collapse2 = _interopRequireDefault(_collapse);

require('codemirror/mode/css/css.js');

require('codemirror/addon/hint/show-hint.js');

require('codemirror/addon/hint/css-hint.js');

var _CodeMirror = require('./common/CodeMirror');

var _CodeMirror2 = _interopRequireDefault(_CodeMirror);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

var Panel = _collapse2['default'].Panel;

var EditorCss = function (_Component) {
  (0, _inherits3['default'])(EditorCss, _Component);

  function EditorCss() {
    var _ref;

    var _temp, _this, _ret;

    (0, _classCallCheck3['default'])(this, EditorCss);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3['default'])(this, (_ref = EditorCss.__proto__ || Object.getPrototypeOf(EditorCss)).call.apply(_ref, [this].concat(args))), _this), _this.onChange = function (value) {
      _this.props.onChange(value.replace(/{\n|\n}|{|}/g, ''));
    }, _this.onKeyUp = function (cm, event) {
      if (!cm.state.completionActive && (event.key.match(/[a-z]/ig) && event.key.length === 1 || event.keyCode === 32)) {
        cm.showHint();
      }
    }, _temp), (0, _possibleConstructorReturn3['default'])(_this, _ret);
  }

  (0, _createClass3['default'])(EditorCss, [{
    key: 'render',
    value: function render() {
      var _this2 = this;

      var props = (0, _objectWithoutProperties3['default'])(this.props, []);
      var value = props.value,
          locale = props.locale;

      ['defaultData', 'onChange'].map(function (key) {
        return delete props[key];
      });
      var newValue = '{\n' + value + '\n}';
      return _react2['default'].createElement(
        Panel,
        (0, _extends3['default'])({}, props, { header: props.header || locale.header }),
        _react2['default'].createElement(_CodeMirror2['default'], {
          value: newValue,
          options: {
            mode: { name: 'css', globalVars: true },
            theme: 'ambiance'
          },
          onKeyUp: this.onKeyUp,
          onBlur: function onBlur(_, __, v) {
            if (v.replace(/\s+/g, '') !== newValue.replace(/\s+/g, '')) {
              _this2.onChange(v);
            }
          }
        })
      );
    }
  }]);
  return EditorCss;
}(_react.Component);

EditorCss.propTypes = {
  value: _propTypes2['default'].string,
  onChange: _propTypes2['default'].func,
  cssName: _propTypes2['default'].string,
  header: _propTypes2['default'].string
};
EditorCss.defaultProps = {
  value: '',
  onChange: function onChange() {}
};
exports['default'] = EditorCss;


EditorCss.componentName = 'EditorCss';
module.exports = exports['default'];